// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.ads.tools.jaxws;

import com.google.common.annotations.VisibleForTesting;

/**
 * A tool capable of generating JAX-WS classes.
 *
 * @author Joseph DiLallo
 */
public class Wsimporter {

  private WsimportToolFactory wsimportToolFactory;

  public Wsimporter() {
    wsimportToolFactory = new WsimportToolFactory();
  }

  /**
   * Testable constructor.
   */
  @VisibleForTesting
  Wsimporter(WsimportToolFactory wsimportToolFactory) {
    this.wsimportToolFactory = wsimportToolFactory;
  }

  /**
   * Calls wsimport on any WSDL except the "master" all of the others were
   * merged into.
   *
   * @param genSrcDir The directory to output generated source files to
   * @param bindingFilePath Fully qualified path to the bindings file for this
   *        WSDL
   * @param wsdlLocation The location, file path or URI, of this WSDL
   */
  public void callWsimportOnNonMaster(String genSrcDir, String bindingFilePath,
      String wsdlLocation) {
    callWsimport(new String[] {"-s", genSrcDir, "-b", bindingFilePath, "-keep", "-quiet",
        wsdlLocation});
  }

  /**
   * Calls wsimport on the "master" WSDL all of the others were merged into.
   *
   * @param genSrcDir The directory to output generated source files to
   * @param bindingFilePath Fully qualified path to the bindings file for the
   *        master WSDL
   * @param wsdlLocation The location, file path or URI, of the WSDL which was
   *        used as the base for the master
   * @param masterFilePath Fully qualified path to the generated master WSDL
   */
  public void callWsimportOnMaster(String genSrcDir, String bindingFilePath, String wsdlLocation,
      String masterFilePath) {
    callWsimport(new String[] {"-s", genSrcDir, "-b", bindingFilePath, "-keep", "-quiet",
        "-wsdllocation", wsdlLocation, masterFilePath});
  }

  /**
   * Makes a call to the wsimport tool.
   *
   * @param args The arguments to pass to wsimport
   */
  private void callWsimport(String[] args) {
    wsimportToolFactory.getWsimportTool().run(args);
  }
}
